1 package attr;
2
3 import java.lang.*;
4 import java.sql.*;
5 import attr.*;
6 import javax.swing.*;
7 import activity.*;
8
9 public abstract class User {
10 protected String userId;
11 protected String password;
12 protected int status;
13
14 public User(String userId) {
15 if (!userId.isEmpty())
16 this.userId = userId;
17 else
18 throw new IllegalArgumentException("Fill in the User ID");
19 }
20
21 public abstract void fetch();
22
23 public String getUserId() {
24 return userId;
25 }
26 public void setStatus(int stts) {
27 this.status = stts;
28 }
29 public void setPassword(String passwd) {
30 if (!passwd.isEmpty())
31 this.password = passwd;
32 else
33 throw new IllegalArgumentException("Fill in the password");
34 }
35
36 public static int checkStatus(String uid, String pass) {
37 int result = -1;
38 String query = "SELECT `userId`, `password`, `status` FROM `login`;";
39 Connection con = null;
40 Statement st = null;
41 ResultSet rs = null;
42 System.out.println(query);
43 try {
44 Class.forName("com.mysql.jdbc.Driver");
45 System.out.println("driver loaded");
46 con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
47 System.out.println("connection done");//connection with database established
48 st = con.createStatement();//create statement
49 System.out.println("statement created");
50 rs = st.executeQuery(query);//getting result
51 System.out.println("results received");
52
53 while(rs.next()) {
54 String userId = rs.getString("userId");
55 String password = rs.getString("password");
56 int status = rs.getInt("status");
57
58 if(userId.equals(uid) && password.equals(pass)) {
59 result = status;
60 }
61 }
62 }
63 catch(Exception ex) {
64 System.out.println("Exception : " +ex.getMessage());
65 ex.printStackTrace();
66 }
67 finally {
68 try {
69 if(rs!=null)
70 rs.close();
71
72 if(st!=null)
73 st.close();
74
75 if(con!=null)
76 con.close();
77 }
78 catch(Exception ex) {}
79 }
80 return result;
81 }
82
83 public void changePassword(ChangePasswordActivity a, String oldPass, String newPass) {
84 String query = "UPDATE `login` SET `password`='"+newPass+"' WHERE (`userId`='"+this.userId+"' AND `password`='"+oldPass+"');";
85 Connection con = null;
86 Statement st = null;
87 System.out.println(query);
88 try {
89 Class.forName("com.mysql.jdbc.Driver");
90 System.out.println("driver loaded");
91 con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
92 System.out.println("connection done");//connection with database established
93 st = con.createStatement();//create statement
94 System.out.println("statement created");
95 int res = st.executeUpdate(query);//insert
96 System.out.println("data inserted");
97 if (res > 0) {
98 JOptionPane.showMessageDialog(null,"Password Updated!");
99 a.setVisible(false);
100 }
101 else {
102 JOptionPane.showMessageDialog(null,"Password didn't match!");
103 }
104 }
105 catch(Exception ex) {
106 System.out.println("Exception : " +ex.getMessage());
107 }
108 finally {
109 try {
110 if(st!=null)
111 st.close();
112
113 if(con!=null)
114 con.close();
115 }
116 catch(Exception ex) {}
117 }
118 }
119 }